const service = require('../service/commentService')
const controller = {
  // 添加
  async addComment(req, res) {
    const {
      parentId,
      uid,
      address,
      content,
      articleId,
      articleTitle,
      username,
      avatar,
      report,
    } = req.body
    let user = { username, avatar }
    let data = await service.addComment(
      parentId,
      uid,
      address,
      content,
      articleId,
      articleTitle,
      user,
      report
    )
    res.send({ ok: 1, data })
  },
  // 查找
  async findComment(req, res) {
    let { articleId } = req.query
    let data = await service.findComment(articleId)
    res.send({ ok: 1, info: data })
  },
  // 删除多个评论
  async delAllComment(req, res) {
    const { ids } = req.body
    let data = await service.delAllComment(ids)
    res.send({ ok: 1, data })
  },
  // 举报用户
  async reportComment(req, res) {
    const { id } = req.query
    let data = await service.reportComment(id)
    res.send({ ok: 1, data })
  },
  // 查找所有的评论
  async findAllComment(req, res) {
    let { page, limit, where = {} } = req.body
    where.articleTitle = where.articleTitle
      ? new RegExp(where.articleTitle)
      : new RegExp('.')
    where.content = where.content ? new RegExp(where.content) : new RegExp('.')
    where.address = where.address ? new RegExp(where.address) : new RegExp('.')
    where.report = where.report ? new RegExp(where.report) : new RegExp('.')
    page = page ? page : '1'
    limit = limit ? limit : '5'
    console.log(where)
    let data = await service.findAllComment(page, limit, where)
    res.send({ ok: 1, data })
  },
}

module.exports = controller
